package servlet.events;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Rating;
import dao.DAOFactory;

public class AddNoteHandler  extends EventHandler {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		
		Rating aRating = getRating(request);
		if (DAOFactory.getRatingDAO().insert(aRating)){
			forward("/jsp/detailsRecipe.jsp?id="+Integer.parseInt(request.getParameter(COMMENT_RECIPE_ID))+"&category_id="+Integer.parseInt(request.getParameter(CATEGORY_ID)), request, response);
		}
	}
	
	private Rating getRating(HttpServletRequest request) {
		Rating aRating = new Rating();
		aRating.setValue(Integer.parseInt(request.getParameter(RATING_VALUE)));
		aRating.setRecipe_id(Integer.parseInt(request.getParameter(COMMENT_RECIPE_ID)));	
		return aRating;
	}
	
	private static final String COMMENT_RECIPE_ID = "recipe_id";
	private static final String RATING_VALUE = "rating";
	private static final String CATEGORY_ID = "category_id";
}